package com.tensquare.gathering.service;

import com.tensquare.gathering.config.MyConfig_1;
import com.tensquare.gathering.dao.GatheringDao;
import com.tensquare.gathering.pojo.Gathering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/26 9:33
 */
@Service
public class GatheringService {
    @Autowired
    private GatheringDao gatheringDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * fetch data by rule id
     *
     * @description  增加活动
     * @param
     * @return
     */
    public void add(Gathering gathering){
        gathering.setId(idWorker.nextId()+"");
        gatheringDao.save(gathering);
    }

    /**
     * fetch data by rule id
     *
     * @description  活动全部列表
     * @param
     * @return
     */
    public List<Gathering> findAll(){
        return gatheringDao.findAll();
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询活动  当此方法第一次运行，在缓存中没有找到对应的value和key的时候
     *               将查询结果放入到缓存中
     * @param
     * @return
     */
    @Cacheable(value = "gathering",key = "#id")
    public Gathering findById(String id){
        return gatheringDao.findById(id).get();
    }

    /**
     * fetch data by rule id
     *
     * @description  修改  清楚缓存的注解
     * @param
     * @return
     */
    @CacheEvict(value = "gathering",key = "#gathering.id")
    public void update(Gathering gathering){
        gatheringDao.save(gathering);
    }

    /**
     * fetch data by rule id
     *
     * @description  删除   清楚缓存的注解
     * @param
     * @return
     */
    @CacheEvict(value = "gathering",key = "#id")
    public void delete(String id){
        gatheringDao.deleteById(id);
    }

    /**
     * fetch data by rule id
     *
     * @description  定义查询条件
     * @param
     * @return
     */
    public Specification<Gathering> createSpecification(Map searchMap){
        return new Specification<Gathering>(){

            @Override
            public Predicate toPredicate(Root<Gathering> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                    predicateList.add(cb.like(root.get("name").as(String.class),"%"+(String) searchMap.get("name")+"%"));
                }
                if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                    predicateList.add(cb.equal(root.get("summary").as(String.class),(String) searchMap.get("summary")));
                }
                if(searchMap.get("sponsor")!=null && !"".equals(searchMap.get("sponsor"))){
                    predicateList.add(cb.equal(root.get("sponsor").as(String.class),(String) searchMap.get("sponsor")));
                }
                if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                    predicateList.add(cb.equal(root.get("image").as(String.class),(String) searchMap.get("image")));
                }
                if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                    predicateList.add(cb.equal(root.get("address").as(String.class),(String) searchMap.get("address")));
                }
                if(searchMap.get("enrolltime")!=null && !"".equals(searchMap.get("enrolltime"))){
                    predicateList.add(cb.equal(root.get("enrolltime").as(String.class),(String) searchMap.get("enrolltime")));
                }
                if(searchMap.get("city")!=null && !"".equals(searchMap.get("city"))){
                    predicateList.add(cb.equal(root.get("city").as(String.class),(String) searchMap.get("city")));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询活动列表
     * @param
     * @return
     */
    public List<Gathering> findSearch(Map searchMap){
        Specification<Gathering> specification = createSpecification(searchMap);
        return gatheringDao.findAll(specification);
    }

    /**
     * fetch data by rule id
     *
     * @description  活动分页
     * @param
     * @return
     */
    public Page<Gathering> findSearch(Map searchMap,int page,int size){
        Specification<Gathering> specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page-1, size);
        return gatheringDao.findAll(specification, pageRequest);
    }

    /**
     * fetch data by rule id
     *
     * @description  根据城市显示分页内容
     * @param
     * @return
     */
    public Page<Gathering> findListByCity(String city,int page,int size){
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return gatheringDao.findListByCity(city,pageRequest);
    }
}
